package main

import (
	"fmt"
	"sync"
)

type worker struct {
	in   chan int
	done chan bool
}

func createWorker(id int) worker {
	c := worker{
		in:   make(chan int),
		done: make(chan bool),
	}
	go doworker(id, c.in, c.done)
	return c
}

func doworker(id int, c chan int, done chan bool) {
	// 	for {
	// 		n, ok := <-c
	// 		if !ok {
	// 			break
	// 		}
	// 		fmt.Printf("Worker %d received %d\n", id, n)
	// 	}

	for n := range c {
		fmt.Printf("Worker %d received %c\n", id, n)
		go func() { done <- true }()
	}
}

func chanDemo() {
	// var c chan int // c == nil
	// c := make(chan int)
	var channels [10]worker

	for i := 0; i < 10; i++ {
		// channels[i] = make(chan int)
		// go worker(i, channels[i])
		channels[i] = createWorker(i)

	}

	var wg sync.WaitGroup
	wg.Add(20)

	// go worker(0, c)
	for i := 0; i < 10; i++ {
		channels[i].in <- 'a' + i
		wg.Done()
	}

	for i := 0; i < 10; i++ {
		channels[i].in <- 'A' + i
		wg.Done()
	}

	// for _, w := range channels {
	// 	<-w.done
	// 	<-w.done
	// }

	wg.Wait()
}

func main() {
	chanDemo()
}
